<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Recharge.php
 * Created on : 2023-4-5 19:20:23
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\controller;

use app\Request;
use haoteam\service\UtilService;
use app\models\Recharge as RechargeModel;
use app\models\UserRecharge;

/**
 * 充值管理
 * Class Recharge
 * Tiger <1192851302@qq.com> 2023-4-5 19:20:23
 */
class Recharge {

    /**
     * 充值记录
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @desc    充值记录
     * @param Request $request
     * @return type
     */
    public function list(Request $request) {
        $post = UtilService::postMore([
                    'date',
                    'status',
                    'search'
        ]);
        return app('json')->successful(UserRecharge::getList($post));
    }

    /**
     * 套餐列表
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @desc    套餐列表
     * @param Request $request
     * @return type
     */
    public function index(Request $request) {
        return app('json')->successful(RechargeModel::getList());
    }

    /**
     * 套餐创建
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    套餐创建
     * @param Request $request
     * @return type
     */
    public function create(Request $request) {
        $post = UtilService::postMore([
                    'buy_nomey',
                    'give_nomey',
        ]);

        try {
            RechargeModel::create($post);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('创建成功');
    }

    /**
     * 套餐更新
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    套餐更新
     * @param Request $request
     * @return type
     */
    public function update(Request $request) {
        $post = UtilService::postMore([
                    'id',
                    'buy_nomey',
                    'give_nomey',
        ]);

        try {
            RechargeModel::update($post, ['id' => $post['id']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('更新成功');
    }

    /**
     * 套餐删除
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc 套餐删除【软删除】
     * @param Request $request
     * @return type
     */
    public function delete(Request $request) {
        $post = UtilService::postMore([
                    'id'
        ]);

        $model = RechargeModel::where(['id' => $post['id']])->find();
        try {
            $model->is_del = 1;
            $model->save();
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('删除成功');
    }

}
